﻿Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class CreateQuiz
    Inherits System.Web.UI.Page
    'Brandon Maroney
    'This page is meant to allow the user to create questions and answers for each question, you can either create a quiz or modify a current quiz from this page

    Private connectionString As String = WebConfigurationManager.ConnectionStrings("Database").ConnectionString
    Public Property BgColor As String ' <----- Wat is this? 
    Protected Sub CreateQuiz_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If CType(PreviousPage.FindControl("quizHiddenField"), HiddenField).Value = "" Then
                fillMainDropList(sender, e)
                quizIDLabel.Text = CType(PreviousPage.FindControl("quizIDHiddenField"), HiddenField).Value
                quizTitleTextBox.Text = CType(PreviousPage.FindControl("titleHiddenField"), HiddenField).Value
                quiztitleLabel.Visible = False
                userNameLabel.Text = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
                userNameHiddenField.Value = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
            Else
                fillMainDropList(sender, e)
                userNameLabel.Text = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
                userNameHiddenField.Value = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
                getQuizID(CType(PreviousPage.FindControl("userIDHiddenField"), HiddenField).Value)
            End If
            'Table1.Rows(1).BgColor = "#ffffff"
            userNameHiddenField.Value = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
            userIDHiddenField.Value = CType(PreviousPage.FindControl("userIDHiddenField"), HiddenField).Value
        End If


    End Sub

    Protected Sub fillMainDropList(ByVal sender As Object, ByVal e As System.EventArgs)
        'using new dim for each item atm, change later if can to elinminate redundancies 
        If Not IsPostBack Then
            Dim newItem As New ListItem
            newItem.Text = "Select a Function"
            mainDropDownList.Items.Add(newItem)
            Dim add As New ListItem
            add.Text = "Add a Question"
            add.Value = "add".ToString
            mainDropDownList.Items.Add(add)
            Dim md As New ListItem
            md.Text = "Modify/Delete a question"
            md.Value = "md".ToString
            mainDropDownList.Items.Add(md)
        End If

    End Sub

    Protected Sub mainSelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mainDropDownList.SelectedIndexChanged
        errorLabel.Text = ""
        newQTextBox.Text = ""
        subDropDownList.Visible = True
        subLabel.Visible = True
        newQTextBox.Visible = True
        If mainDropDownList.SelectedValue = "add" Then
            addButton.Visible = True
            updateButton.Visible = False
            deleteButton.Visible = False
            subDropDownList.Visible = False

            mainAnswerDDlist.Visible = False
            answersTextBox.Visible = False
            modifyAnswerList.Visible = False
        Else
            fillMainAnswerDDList(sender, e)
            fillSubDropDownList(sender, e)
            updateButton.Visible = True
            deleteButton.Visible = True
            addButton.Visible = False
            subDropDownList.Visible = True
            answerLabel.Visible = False
        End If
        addAnswerButton.Visible = False
        updateAnswerButton.Visible = False
        deleteAnswerButton.Visible = False
    End Sub

    Protected Sub fillSubDropDownList(sender As Object, e As EventArgs)
        subDropDownList.Items.Clear()
        errorLabel.Text = ""
        Dim selectSQL As String = "SELECT * FROM QuestionSelect WHERE '" & quizIDLabel.Text & "' = QuestionSelect.Quiz_Id"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(selectSQL, myConnection)
        Dim myAdapter As New SqlDataAdapter(myCommand)
        Dim pubsDataSet As New DataSet

        Try
            myConnection.Open()
            myAdapter.Fill(pubsDataSet, "QuestionSelect")

            Dim firstItem As New ListItem
            firstItem.Text = "Select a Question To Modify"
            firstItem.Value = "Select a Question To Modify".ToString
            subDropDownList.Items.Add(firstItem)

            For Each row As DataRow In pubsDataSet.Tables("QuestionSelect").Rows
                Dim newItem As New ListItem
                newItem.Text = row("Question")
                newItem.Value = row("question_Id").ToString
                subDropDownList.Items.Add(newItem)
            Next

        Catch ex As Exception
            errorLabel.Text = "Error Getting Questions " & ex.Message
        Finally
            myConnection.Close()
            newQTextBox.Text = ""
        End Try
    End Sub

    Protected Sub subDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles subDropDownList.SelectedIndexChanged
        If mainDropDownList.SelectedValue = "md" Then
            newQTextBox.Text = subDropDownList.SelectedItem.ToString
        End If
        answersTextBox.Text = ""
        fillMainAnswerDDList(sender, e)
        answerLabel.Visible = True
        mainAnswerDDlist.Visible = True
        answersTextBox.Visible = True
    End Sub

    Protected Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
        If newQTextBox.Text = "" Then
            errorLabel.Text = "Please Insert a New Question To Add"
        Else
            Dim insertSQL As New StringBuilder
            insertSQL.Append("INSERT INTO QuestionSelect(Question,Quiz_Id)")
            insertSQL.Append("VALUES (@Question,@Quiz_Id)")

            Dim myConnection As New SqlConnection(connectionString)
            Dim myCommand As New SqlCommand(insertSQL.ToString, myConnection)
            'we must connect parameters to the firls using myCommand.Parameters

            With myCommand.Parameters
                .AddWithValue("@Question", newQTextBox.Text)
                .AddWithValue("@Quiz_Id", quizIDLabel.Text)
            End With
            Dim successBoolean As Boolean = True

            Try
                myConnection.Open()
                successBoolean = myCommand.ExecuteNonQuery
                errorLabel.Text = "New Quetion Inserted"
                addButton.PostBackUrl = "CreateQuiz.aspx"
            Catch ex As Exception
                successBoolean = False
                errorLabel.Text = "Error inserting Question. " & ex.Message
            Finally
                myConnection.Close()
            End Try
            newQTextBox.Text = ""
        End If
    End Sub

    Protected Sub updateButton_Click(sender As Object, e As EventArgs) Handles updateButton.Click
        errorLabel.Text = ""
        Dim updateSQL As New StringBuilder
        updateSQL.Append("UPDATE QuestionSelect SET ")
        updateSQL.Append("Question = @Question")
        updateSQL.Append(" WHERE question_Id = @question_Id_Original")
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(updateSQL.ToString, myConnection)

        With myCommand.Parameters
            .AddWithValue("@Question", newQTextBox.Text)
            .AddWithValue("@question_Id_original", subDropDownList.SelectedValue)
        End With

        Dim successBoolean As Boolean = True

        Try
            myConnection.Open()
            successBoolean = myCommand.ExecuteNonQuery
            errorLabel.Text = "Question Updated"
        Catch ex As Exception
            successBoolean = False
            errorLabel.Text = "Error updating question. " & ex.Message
        Finally
            myConnection.Close()
        End Try

        If successBoolean Then
            fillSubDropDownList(sender, e)
            subDropDownList_SelectedIndexChanged(sender, e)
        End If
    End Sub

    Protected Sub deleteButton_Click(sender As Object, e As EventArgs) Handles deleteButton.Click
        errorLabel.Text = ""
        Dim deleteQuizSQL As String
        deleteQuizSQL = " DELETE FROM QuestionSelect WHERE question_Id=@question_Id"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(deleteQuizSQL, myConnection)

        myCommand.Parameters.AddWithValue("@question_Id", subDropDownList.SelectedValue)

        Dim successQuestionBoolean As Boolean = True

        Try
            myConnection.Open()
            successQuestionBoolean = myCommand.ExecuteNonQuery
        Catch ex As Exception
            successQuestionBoolean = False
            errorLabel.Text = "Error deleting questions. " & ex.Message
        Finally
            myConnection.Close()
        End Try

        If successQuestionBoolean Then
            fillSubDropDownList(sender, e)
            subDropDownList_SelectedIndexChanged(sender, e)
        End If
    End Sub

    Protected Sub getQuizID(ByVal userID As String)
        Dim getIDSQL As String = "SELECT * FROM QuestionMain WHERE DateTime ='" & DateAndTime.Now & "'"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(getIDSQL.ToString, myConnection)
        Dim myReader As SqlDataReader
        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader
            myReader.Read()
            quizIDLabel.Text = myReader("Quiz_Id")
        Catch ex As Exception
            quizIDLabel.Text = "cant get quizid"
        Finally
            myConnection.Close()
        End Try
    End Sub

    Protected Sub titleButton_Click(sender As Object, e As EventArgs) Handles titleButton.Click
        Dim updateSQL As New StringBuilder
        updateSQL.Append("UPDATE QuestionMain SET")
        updateSQL.Append(" QuizTitle = @QuizTitle")
        updateSQL.Append(" WHERE Quiz_Id = @Quiz_Id_original")
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(updateSQL.ToString, myConnection)

        With myCommand.Parameters
            .AddWithValue("@QuizTitle", quizTitleTextBox.Text)
            .AddWithValue("@Quiz_Id_original", quizIDLabel.Text)
        End With

        Dim successTitleBoolean As Boolean = True

        Try
            myConnection.Open()
            successTitleBoolean = myCommand.ExecuteNonQuery
            errorLabel.Text = "Title Created"
        Catch ex As Exception
            successTitleBoolean = False
            errorLabel.Text = "Error Creating Title" & ex.Message
        Finally
            myConnection.Close()
        End Try
    End Sub

    Protected Sub fillMainAnswerDDList(sender As Object, e As EventArgs)
        mainAnswerDDlist.Items.Clear()
        Dim newItem As New ListItem
        newItem.Text = "Select a Function"
        mainAnswerDDlist.Items.Add(newItem)
        Dim add As New ListItem
        add.Text = "Add an Answer"
        add.Value = "add".ToString
        mainAnswerDDlist.Items.Add(add)
        Dim md As New ListItem
        md.Text = "Modify/Delete an Answer"
        md.Value = "md".ToString
        mainAnswerDDlist.Items.Add(md)
    End Sub

    Protected Sub fillModifyAnswerList(sender As Object, e As EventArgs)
        modifyAnswerList.Items.Clear()
        errorLabel.Text = ""
        answersTextBox.Text = ""
        Dim selectSQL As String = "SELECT * FROM Answers WHERE questionID = '" & subDropDownList.SelectedValue & "'"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(selectSQL, myConnection)
        Dim myAdapter As New SqlDataAdapter(myCommand)
        Dim DataSet As New DataSet

        Try
            myConnection.Open()
            myAdapter.Fill(DataSet, "Answers")

            Dim firstItem As New ListItem
            firstItem.Text = "Select an Answer To Modify"
            firstItem.Value = "Select an Answer To Modify".ToString
            modifyAnswerList.Items.Add(firstItem)

            For Each row As DataRow In DataSet.Tables("Answers").Rows
                Dim newItem As New ListItem
                newItem.Text = row("Answer")
                newItem.Value = row("Id").ToString
                modifyAnswerList.Items.Add(newItem)
            Next

        Catch ex As Exception
            errorLabel.Text = "Error Getting Answers " & ex.Message
        Finally
            myConnection.Close()
            answersTextBox.Text = ""
        End Try
    End Sub

    Protected Sub mainAnswerDDlist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles mainAnswerDDlist.SelectedIndexChanged
        errorLabel.Text = ""
        answersTextBox.Text = ""

        If mainAnswerDDlist.SelectedValue = "md" Then
            modifyAnswerList.Visible = True
            updateAnswerButton.Visible = True
            deleteAnswerButton.Visible = True
            addAnswerButton.Visible = False
            fillModifyAnswerList(sender, e)
        Else
            modifyAnswerList.Visible = False
            addAnswerButton.Visible = True
            updateAnswerButton.Visible = False
            deleteAnswerButton.Visible = False
        End If
        answerLabel.Visible = True
    End Sub

    Protected Sub addAnswerButton_Click(sender As Object, e As EventArgs) Handles addAnswerButton.Click
        If answersTextBox.Text = "" Then
            errorLabel.Text = "Please Insert a New Question To Add"
        Else
            Dim insertSQL As New StringBuilder
            insertSQL.Append("INSERT INTO Answers(Answer,questionID,Correct)")
            insertSQL.Append("VALUES (@Answer,@questionID,@Correct)")

            Dim myConnection As New SqlConnection(connectionString)
            Dim myCommand As New SqlCommand(insertSQL.ToString, myConnection)
            'we must connect parameters to the firls using myCommand.Parameters

            With myCommand.Parameters
                .AddWithValue("@Answer", answersTextBox.Text)
                .AddWithValue("@questionID", subDropDownList.SelectedValue)
                .AddWithValue("@Correct", correctHiddenField.Value)

            End With
            Dim successBoolean As Boolean = True

            Try
                myConnection.Open()
                successBoolean = myCommand.ExecuteNonQuery
                errorLabel.Text = "New Answer Inserted"
            Catch ex As Exception
                successBoolean = False
                errorLabel.Text = "Error inserting Answer. " & ex.Message
            Finally
                myConnection.Close()
            End Try
        End If
    End Sub


    Protected Sub modifyAnswerList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles modifyAnswerList.SelectedIndexChanged
        Dim getYNSQL As String = "SELECT * FROM Answers WHERE Id ='" & modifyAnswerList.SelectedValue & "'"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(getYNSQL.ToString, myConnection)
        Dim myReader As SqlDataReader
        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader
            myReader.Read()
        Catch ex As Exception
            quizIDLabel.Text = "cant get Correct Answer"
        Finally
            myConnection.Close()
            answerLabel.Visible = True
        End Try

        answersTextBox.Text = modifyAnswerList.SelectedItem.ToString
    End Sub

    Protected Sub updateAnswerButton_Click(sender As Object, e As EventArgs) Handles updateAnswerButton.Click
        errorLabel.Text = ""
        Dim updateSQL As New StringBuilder
        updateSQL.Append("UPDATE Answers SET ")
        updateSQL.Append("Answer = @Answer")
        updateSQL.Append(" WHERE Id = @Id_Original")
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(updateSQL.ToString, myConnection)

        With myCommand.Parameters
            .AddWithValue("@Answer", answersTextBox.Text)
            .AddWithValue("@Id_original", modifyAnswerList.SelectedValue)
        End With

        Dim successBoolean As Boolean = True

        Try
            myConnection.Open()
            successBoolean = myCommand.ExecuteNonQuery
            errorLabel.Text = "Answer Updated"
        Catch ex As Exception
            successBoolean = False
            errorLabel.Text = "Error updating Answer. " & ex.Message
        Finally
            myConnection.Close()
        End Try

        If successBoolean Then
            fillModifyAnswerList(sender, e)
            modifyAnswerList_SelectedIndexChanged(sender, e)
        End If
    End Sub

    Protected Sub deleteAnswerButton_Click(sender As Object, e As EventArgs) Handles deleteAnswerButton.Click
        errorLabel.Text = ""
        Dim deleteQuizSQL As String
        deleteQuizSQL = " DELETE FROM Answers WHERE Id=@Id"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(deleteQuizSQL, myConnection)

        myCommand.Parameters.AddWithValue("@Id", modifyAnswerList.SelectedValue)

        Dim successQuestionBoolean As Boolean = True

        Try
            myConnection.Open()
            successQuestionBoolean = myCommand.ExecuteNonQuery
        Catch ex As Exception
            successQuestionBoolean = False
            errorLabel.Text = "Error deleting answer. " & ex.Message
        Finally
            myConnection.Close()
        End Try

        If successQuestionBoolean Then
            fillModifyAnswerList(sender, e)
            modifyAnswerList_SelectedIndexChanged(sender, e)
        End If
    End Sub

    Protected Sub homeButton_Click(sender As Object, e As EventArgs) Handles homeButton.Click
        If quizTitleTextBox.Text = "" Then
            errorLabel.Text = "Please enter a title before leaving"
            errorLabel.ForeColor = Drawing.Color.Red
        End If
    End Sub
End Class
